#! /bin/sh
#This is a user defined start script file
WORKING_ROOT=$(
  cd $(dirname "$0") || exit
  cd ..
  pwd
)

## %{APPLICATION_NAME} should be replaced
APPLICATION_NAME=%{APPLICATION_NAME}
# %{CLASSPATH} should be replaced
CLASSPATH=%{CLASSPATH}
# %{MAIN_CLASS} should be replaced
MAIN_CLASS=%{MAIN_CLASS}
# %{JVM_ARGS} should be replaced
JVM_ARGS="%{JVM_ARGS}"
# %{APPLICATION_ARGS} should be replaced
APPLICATION_ARGS=%{APPLICATION_ARGS}

##############################################################################
# 描述:
#   该脚本用于启动应用
#	$0 start-启动;stop-停止;restart-重启;
#
# 步骤:
# 	1) 参数1判定脚本执行方式
#	2) JAVA_HOME check
#	3) 启动程序
#
##############################################################################
COMMAND="start"
PID_FILE="${WORKING_ROOT}/.pid"

FRONT_IND="false"

JMX_IND="false"
#jvm options, like -Dspring.profiles.active=dev,h2
JVM_OPTIONS="-Xms512m -Xmx2048m -Doracle.jdbc.J2EE13Compliant=true -Dfile.encoding=UTF-8 ${JVM_ARGS}"

#jmx options
JMX_OPTIONS="-Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.port=18090 -Dcom.sun.management.jmxremote.ssl=false -Dcom.sun.management.jmxremote.authenticate=false"


usage() {
  echo "$1" "用法:"
  echo "  --help|-h 显示此帮助"
  echo "	--front|-f 用于以前台模式启动，否则以后端模式启动，该参数应置于start|stop|restart前"
  echo "	start	启动应用"
  echo "	stop	停止应用"
  echo "	restart 停止并启动应用"
  exit 0
}

for arg in "$@"; do
  case ${arg} in
  --help | -h)
    usage "$0"
    shift
    continue
    ;;
  --front | -f)
    FRONT_IND="true"
    shift
    continue
    ;;
  --jmx)
    JMX_IND="true"
    shift
    continue
    ;;
  esac
  COMMAND=${arg}
  shift
  break
done

ARGS="$APPLICATION_ARGS $@"

PID=
##read pid from file: .pid
getPidByFile() {
  if [ -f "${PID_FILE}" ]; then
    read -r PID <"${PID_FILE}"
  fi
}

check_application() {
  getPidByFile
  if [ "${PID}" != "" ]; then
    echo "[RC=1, Error]"
    echo "[Message=application is running]"
    echo "$(date +'%Y-%m-%d %T') - ${APPLICATION_NAME}应用已经启动，不允许重复启动. PID: ${PID}"
    exit 1
  fi
}

stop_application() {
  getPidByFile
  if [ "${PID}" = "" ]; then
    echo "[Message=application already stopped]"
    echo "$(date +'%Y-%m-%d %T') - ${APPLICATION_NAME}应用未启动或已停止. PID: ${PID}"
  else
    kill -9 "${PID}"

    if [ $? = 0 ]; then
      rm -Rf "${PID_FILE}"
      echo "[RC=0, Success]"
      echo "[Message=application stop successfully]"
      echo "$(date +'%Y-%m-%d %T') - ${APPLICATION_NAME}应用停止成功. PID: ${PID}"
      return 0
    else
      echo "[RC=3, Error]"
      echo "[Message=application stop failed]"
      echo "$(date +'%Y-%m-%d %T') - ${APPLICATION_NAME}应用停止失败. PID: ${PID}"
      exit 3
    fi
  fi
}

start_application() {
  if [ "${JAVA_HOME}" = "" ]; then
    echo "JAVA_HOME not set correctly."
    exit 5
  fi

  JAVA="${JAVA_HOME}/bin/java"

  LANG=zh_CN.UTF-8

  if [ "${JMX_IND}" = "false" ]; then
    JMX_OPTIONS=
  fi

  FULL_ARGS="-cp ${WORKING_ROOT}:${WORKING_ROOT}/conf:${CLASSPATH} ${JVM_OPTIONS} ${JMX_OPTIONS} ${MAIN_CLASS}"

  if [ "${FRONT_IND}" = "false" ]; then
    # shellcheck disable=SC2086
    nohup "${JAVA}" ${FULL_ARGS} ${ARGS} >/dev/null 2>&1 &
  else
    # shellcheck disable=SC2086
    "${JAVA}" ${FULL_ARGS} ${ARGS}
  fi

  PID=$!
  LAST_RC=$?

  if [ "${PID}" != "" ]; then
    echo "${PID}" >"${PID_FILE}"
    echo "[RC=0; Success]"
    echo "[Message=application start successfully]"
    echo "$(date +'%Y-%m-%d %T') - ${APPLICATION_NAME}应用成功启动. PID: ${PID}"
    exit 0
  elif [ "${LAST_RC}" = "0" ]; then
    echo "[RC=0; Success]"
    echo "[Message=application start and finished]"
    echo "$(date +'%Y-%m-%d %T') - ${APPLICATION_NAME}应用执行完毕."
    exit 0
  else
    echo "[RC=6; Error]"
    echo "[Message=application start failed]"
    echo "$(date +'%Y-%m-%d %T') - ${APPLICATION_NAME}应用启动失败. 请检查."
    exit 6
  fi
}

if [ "${COMMAND}" = "start" ]; then
  check_application
  start_application "${ARGS}"

elif [ "${COMMAND}" = "stop" ]; then
  stop_application

elif [ "${COMMAND}" = "restart" ]; then
  stop_application
  check_application
  start_application "${ARGS}"
else
  echo "[RC=4, Error]"
  echo "[Message=the input parameter is WRONG]"
  echo "$(date +'%Y-%m-%d %T') - 输入参数有误: ${COMMAND} , 请输入 start|stop|restart"
  echo "start - 启动; stop - 停止; restart - 重启"
  exit 4
fi
